// pages/recorddetail/recorddetail.js
var utils = require('../../utils/util.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    repairRecordId: '',
    userInfo: null,
    url: '',
    detail: null,
    oldImage: [],
    files: [],
    accessoryDescribe: '',
    repairRecordRemark: '',
    faultDescribe: '',
    assistName: '',
    assistPhone: '',
    repairStatus: '',
    showSuccess: false,
    showModal: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      url: getApp().globalData.url,
      userInfo: getApp().globalData.yukaiUser
    });
    this.getDetail(options.repairRecordId);
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  } ,
  /**
   * 获取维修详情
   */
  getDetail: function (repairRecordId) {
    wx.showLoading({
      title: '加载中',
    });
    var userInfo = this.data.userInfo ? this.data.userInfo : getApp().getUser();
    var url = this.data.url ? this.data.url : getApp().getUrl();
    var _self = this;
    wx.request({
      url: url + '/repairRecord/findByRepairId',
      data: {
        token: userInfo.token,
        repairRecordId: repairRecordId
      },
      method: 'GET',
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success: function (res) {
        console.log(res.data);
        var data = res.data;
        switch (data.code) {
          case 200:
            _self.setData({
              detail: _self.convert(data.obj)
            });
            wx.hideLoading();
            break;
          case 412:
            wx.hideLoading();
            utils.alertBox(data.msg);
            break;
          case 422:
            wx.hideLoading();
            utils.toLogin();
            break;
          default:
            wx.hideLoading();
            break;
        }
      },
      fail: function () {
        wx.hideLoading();
        utils.alertBox('请求失败，请检查网络');
      }
    });
  },
  convert: function (obj) {
    var photo = obj.faultPhoto.split('-');
    photo.pop();
    this.setData({
      oldImage: photo
    });
    return {
      equipmentName: obj.equipmentName,
      equipmentSerial: obj.equipmentSerial,
      eqiupmentModel: obj.eqiupmentModel,
      hospitalName: obj.hospitalName,
      departmentName: obj.departmentName,
      staffName: obj.staffName,
      repairDate: utils.formatTime(new Date(obj.repairDate)),
      hospitalName: obj.hospitalName,
      faultDescribe: obj.faultDescribe,
      repairRecordRemark: obj.repairRecordRemark,
      accessoryDescribe: obj.accessoryDescribe,
      acceptanceStatus: obj.acceptanceStatus,
      repairStatus: obj.repairStatus,
      repairRecordId: obj.repairRecordId,
      assistName: obj.assistName,
      assistPhone: obj.assistPhone,
      errorCode: obj.errorCode
    };
  },
  //预览图片
  previewOldImage: function (e) {
    wx.previewImage({
      current: e.currentTarget.id, // 当前显示图片的http链接
      urls: this.data.oldImage // 需要预览的图片http链接列表
    })
  }
})